package com.vinjogames.util;



import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.preference.PreferenceManager;

import com.vinjogames.TankGame.R;

public class SoundManager {

	private Context mContext;
	private SharedPreferences mPreferences;
	private SoundPool mSounds;
	private MediaPlayer mMusic;

	private int mLoopID;
	
	public int BOMB_SOUND_ID;
	
	private SoundManager(){};

	private static class SoundManagerHolder {
		public static final SoundManager instance = new SoundManager();
	}

	public static SoundManager getInstance(){
		return SoundManagerHolder.instance;
	}

	public void LoadResources(Context context){
		mSounds = new SoundPool(3,AudioManager.STREAM_MUSIC,0);
		mContext = context;
		mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
		BOMB_SOUND_ID = mSounds.load(mContext, R.raw.bomb,1);
	}
	
	public void PlaySound(int soundId, boolean loop){
		if(mPreferences.getBoolean(Preferences.SOUND_KEY, Preferences.SOUND_DEFAULT)){
			int streamID = mSounds.play(soundId, 0.7f, 0.7f, loop ? 1:0, loop ? -1:0, 1.0f);
			if(loop){
				mLoopID = streamID;
			}
		}
	}

	public void PlaySound(int soundId){
		PlaySound(soundId, false);
	}

	public void StopLoopingSound(){
		mSounds.stop(mLoopID);
	}

	public void StartMusic(){
		if(mPreferences.getBoolean(Preferences.MUSIC_KEY, Preferences.MUSIC_DEFAULT)){
			if(mMusic == null || mMusic.isPlaying()) return;
			mMusic.setLooping(true);
			mMusic.start();
		}

	}
	
	public void PauseMusic(){
		if(mMusic == null) return;
		if(mMusic.isPlaying()){
			mMusic.pause();
		}
	}

	public void PauseSounds(){
		mSounds.autoPause();
	}

	public void ResumeSounds(){
		mSounds.autoResume();
	}
}
